import template from './auth.html'

class AuthController {
    roles = [{ id: 'student', name: '学生' }, { id: 'teacher', name: '教师' }, { id: 'department', name: '院系管理员' }, { id: 'school', name: '学校管理员' },{ id: 'branch', name: '支部管理员' }, { id: 'tourist', name: '游客' },]
    methods = [{ id: 'create', name: '新增' }, { id: 'query', name: '查询' }, { id: 'update', name: '更新' }, { id: 'delete', name: '删除' },]
    constructor($http, $element, $uibModal, $q, $mdToast) {
        "ngInject";
        this.$http = $http
        let $ctrl = this
        this.$element = $element
        this.$uibModal = $uibModal
        $http.post(':root/auth/getMeta').then(res => {
            $ctrl.meta = res.data
        })
        this.showDialog = function (entity,template) {
            this.$uibModal.open({
                templateUrl: template, size: 'xl', resolve: {
                    data: function () {
                        let qArr = []
                        let resMap = {}
                        $ctrl.roles.forEach(r => {
                            qArr.push($ctrl.$http.post(':root/auth/getWithEntity', { entity: entity.className, role: r.id }).then(function (res) {
                                return res.data;
                            }).then(res => {
                                resMap[r.id] = res || { accessMap: {} };
                            }))
                        })
                        return $q.all(qArr).then(function () {
                            return resMap;
                        })
                    }
                }, controller: function ($scope, data, $http, $q, $uibModalInstance) {
                    "ngInject";
                    $scope.roles = $ctrl.roles;
                    $scope.methods = $ctrl.methods;
                    $scope.entity = entity;
                    $scope.data = data;
                    this.ok = function (data) {
                        let qs = []
                        angular.forEach(data, (v, k) => {
                            if (v) {
                                v.role = k;
                                v.entity = entity.className
                                qs.push($http.post(':root/auth/setEntityPermission', { permission: JSON.stringify(v) }));
                            }
                        })
                        $q.all(qs).then(function () {
                            $uibModalInstance.close();
                        })
                    }
                    this.cancel = function () {
                        $uibModalInstance.close();
                    }
                    this.batch = function (target, field, value, mapKeys) {
                        angular.isArray(mapKeys) && angular.forEach(mapKeys, k => {
                            target[k] = target[k] || {}
                        });
                        angular.forEach(target, v => {
                            v[field] = value;
                        })
                    }
                }, controllerAs: '$ctrl'
            })
        }
        this.directSave = function () {
            $http.post(':root/auth/save').then(response => {
                $mdToast.show($mdToast.simple()
                    .textContent(response.data))
            }).catch(response => {
                $mdToast.show($mdToast.simple()
                    .textContent(response.data.message))
            })
        }
        this.startSave = function () {
            this.$uibModal.open({
                template: '<angular-filemanager></angular-filemanager><a class="btn btn-success" ng-click="$ctrl.ok()">OK</a>', size: 'xl',
                controller: function ($rootScope, $uibModalInstance) {
                    "ngInject";
                    this.ok = function () {
                        console.log($rootScope.selectedModalPath, $rootScope.selectedFile)
                        let file = ''
                        angular.forEach($rootScope.selectedModalPath, v => {
                            file += ('/' + v);
                        })
                        if ($rootScope.selectedFile[0])
                            file += ('/' + $rootScope.selectedFile[0].model.name);
                        $http.post(':root/auth/saveToDBFile', { file: file }).then(response => {
                            $uibModalInstance.close();
                        })
                    }
                }, controllerAs: '$ctrl'
            })
        }
    }

}
let AuthComponent = {
    template,
    restrict: 'E',
    controller: AuthController
}

export default AuthComponent
